package Interfaz;

import Cliente.OpcionesDelCliente;
import java.io.BufferedReader;
import java.io.PrintWriter;
import javax.swing.table.DefaultTableModel;

/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */

/*
 * PantallaManejoFTP.java
 *
 * Created on 20-mar-2012, 16:06:50
 */
/**
 *
 * @author Administrador
 */
public class PantallaManejoFTP extends javax.swing.JFrame {

    PrintWriter salida;
    BufferedReader entrada;
    String ultimoDirectorioLocal;
    String ultimoDirectorioRemoto;

    public PantallaManejoFTP(PrintWriter out, BufferedReader in) {
        salida = out;
        entrada = in;
        initComponents();
        jTreeLocal.setModel(OperacionesInterfaz.treeFinal("/", false, entrada, salida));
        jTreeRemoto.setModel(OperacionesInterfaz.treeFinal("/", true, entrada, salida));

    }

    public PantallaManejoFTP() {

        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    public void deshabilitarBotones() {
        botonBorrarFichero.setEnabled(false);
        botonBorrarDirectorio.setEnabled(false);
        botonCrearDirectorio.setEnabled(false);
        borrarUsuario.setEnabled(false);
        crearUsuario.setEnabled(false);

    }
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        botonSubirFichero = new javax.swing.JButton();
        botonBorrarFichero = new javax.swing.JButton();
        botonDescargarFichero = new javax.swing.JButton();
        botonCrearDirectorio = new javax.swing.JButton();
        botonBorrarDirectorio = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTreeLocal = new javax.swing.JTree();
        jLabel2 = new javax.swing.JLabel();
        textoDirectorioLocal = new javax.swing.JTextField();
        textoDirectorioRemoto = new javax.swing.JTextField();
        jScrollPane3 = new javax.swing.JScrollPane();
        tablaDirectorioLocal = new javax.swing.JTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTreeRemoto = new javax.swing.JTree();
        jScrollPane4 = new javax.swing.JScrollPane();
        tablaDirectorioRemoto = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenu3 = new javax.swing.JMenu();
        crearUsuario = new javax.swing.JMenuItem();
        borrarUsuario = new javax.swing.JMenuItem();
        jMenu4 = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(900, 600));

        botonSubirFichero.setText("Subir Fichero");
        botonSubirFichero.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonSubirFicheroActionPerformed(evt);
            }
        });

        botonBorrarFichero.setText("Borrar Fichero");
        botonBorrarFichero.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonBorrarFicheroActionPerformed(evt);
            }
        });

        botonDescargarFichero.setText("Descargar Fichero");
        botonDescargarFichero.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonDescargarFicheroActionPerformed(evt);
            }
        });

        botonCrearDirectorio.setText("Crear Directorio");
        botonCrearDirectorio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonCrearDirectorioActionPerformed(evt);
            }
        });

        botonBorrarDirectorio.setText("Borrar Directorio");
        botonBorrarDirectorio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonBorrarDirectorioActionPerformed(evt);
            }
        });

        jTreeLocal.addTreeExpansionListener(new javax.swing.event.TreeExpansionListener() {
            public void treeCollapsed(javax.swing.event.TreeExpansionEvent evt) {
            }
            public void treeExpanded(javax.swing.event.TreeExpansionEvent evt) {
                jTreeLocalTreeExpanded(evt);
            }
        });
        jTreeLocal.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
            public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {
                jTreeLocalValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(jTreeLocal);

        jLabel2.setText("Directorio Remoto");

        textoDirectorioLocal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                textoDirectorioLocalActionPerformed(evt);
            }
        });

        textoDirectorioRemoto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                textoDirectorioRemotoActionPerformed(evt);
            }
        });

        tablaDirectorioLocal.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Nombre", "Tamaño", "Fecha de modificación", "Permisos", "Oculto"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane3.setViewportView(tablaDirectorioLocal);

        jTreeRemoto.addTreeExpansionListener(new javax.swing.event.TreeExpansionListener() {
            public void treeCollapsed(javax.swing.event.TreeExpansionEvent evt) {
            }
            public void treeExpanded(javax.swing.event.TreeExpansionEvent evt) {
                jTreeRemotoTreeExpanded(evt);
            }
        });
        jTreeRemoto.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
            public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {
                jTreeRemotoValueChanged(evt);
            }
        });
        jScrollPane2.setViewportView(jTreeRemoto);

        tablaDirectorioRemoto.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Nombre", "Tamaño", "Fecha de modificación", "Permisos", "Oculto"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, true, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane4.setViewportView(tablaDirectorioRemoto);

        jLabel1.setText("Directorio Local");

        jMenu1.setText("Archivo");

        jMenuItem3.setText("Salir");
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem3);

        jMenuBar1.add(jMenu1);

        jMenu3.setText("Usuario");

        crearUsuario.setText("Crear Usuario");
        crearUsuario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                crearUsuarioActionPerformed(evt);
            }
        });
        jMenu3.add(crearUsuario);

        borrarUsuario.setText("Borrar Usuario");
        borrarUsuario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                borrarUsuarioActionPerformed(evt);
            }
        });
        jMenu3.add(borrarUsuario);

        jMenuBar1.add(jMenu3);

        jMenu4.setText("Ayuda");
        jMenuBar1.add(jMenu4);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(4, 4, 4)
                        .addComponent(botonSubirFichero, javax.swing.GroupLayout.DEFAULT_SIZE, 106, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(botonBorrarFichero, javax.swing.GroupLayout.DEFAULT_SIZE, 106, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(botonDescargarFichero, javax.swing.GroupLayout.DEFAULT_SIZE, 124, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(botonCrearDirectorio, javax.swing.GroupLayout.DEFAULT_SIZE, 114, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(botonBorrarDirectorio, javax.swing.GroupLayout.DEFAULT_SIZE, 116, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 95, Short.MAX_VALUE)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 95, Short.MAX_VALUE)
                            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 95, Short.MAX_VALUE)
                            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 95, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane4, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 487, Short.MAX_VALUE)
                            .addComponent(textoDirectorioRemoto, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 487, Short.MAX_VALUE)
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 487, Short.MAX_VALUE)
                            .addComponent(textoDirectorioLocal, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 487, Short.MAX_VALUE))))
                .addGap(32, 32, 32))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botonBorrarFichero)
                    .addComponent(botonDescargarFichero)
                    .addComponent(botonCrearDirectorio)
                    .addComponent(botonBorrarDirectorio)
                    .addComponent(botonSubirFichero))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(textoDirectorioLocal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane3, 0, 0, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 162, Short.MAX_VALUE))
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(textoDirectorioRemoto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 162, Short.MAX_VALUE)
                    .addComponent(jScrollPane4, 0, 0, Short.MAX_VALUE))
                .addGap(13, 13, 13))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTreeLocalTreeExpanded(javax.swing.event.TreeExpansionEvent evt) {//GEN-FIRST:event_jTreeLocalTreeExpanded
        OperacionesInterfaz.treeExpanded(evt, false, entrada, salida);
    }//GEN-LAST:event_jTreeLocalTreeExpanded

    private void jTreeLocalValueChanged(javax.swing.event.TreeSelectionEvent evt) {//GEN-FIRST:event_jTreeLocalValueChanged

        ultimoDirectorioLocal = OperacionesInterfaz.mostrarArchivosLocal(evt, tablaDirectorioLocal);
    }//GEN-LAST:event_jTreeLocalValueChanged

    private void jTreeRemotoValueChanged(javax.swing.event.TreeSelectionEvent evt) {//GEN-FIRST:event_jTreeRemotoValueChanged
        ultimoDirectorioRemoto = OperacionesInterfaz.mostrarArchivosServidor(evt, tablaDirectorioRemoto, entrada, salida);
    }//GEN-LAST:event_jTreeRemotoValueChanged

    private void jTreeRemotoTreeExpanded(javax.swing.event.TreeExpansionEvent evt) {//GEN-FIRST:event_jTreeRemotoTreeExpanded
        OperacionesInterfaz.treeExpanded(evt, true, entrada, salida);
    }//GEN-LAST:event_jTreeRemotoTreeExpanded

    private void botonSubirFicheroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonSubirFicheroActionPerformed
        DefaultTableModel modelo = (DefaultTableModel) tablaDirectorioLocal.getModel();
        int filaSeleccionada = tablaDirectorioLocal.getSelectedRow();
        String nombreArchivo = (String) modelo.getValueAt(filaSeleccionada, 0);
        System.out.println(nombreArchivo);
        OpcionesDelCliente.subirFichero(entrada, salida, ultimoDirectorioLocal + "/" + nombreArchivo, ultimoDirectorioRemoto);
    }//GEN-LAST:event_botonSubirFicheroActionPerformed

    private void botonBorrarFicheroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonBorrarFicheroActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_botonBorrarFicheroActionPerformed

    private void botonDescargarFicheroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonDescargarFicheroActionPerformed

        DefaultTableModel modelo = (DefaultTableModel) tablaDirectorioRemoto.getModel();
        int filaSeleccionada = tablaDirectorioRemoto.getSelectedRow();
        String nombreArchivo = (String) modelo.getValueAt(filaSeleccionada,0); 
        OpcionesDelCliente.descargarArchivo(entrada, salida, ultimoDirectorioRemoto + "/" + nombreArchivo, ultimoDirectorioLocal);
    }//GEN-LAST:event_botonDescargarFicheroActionPerformed

    private void botonCrearDirectorioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonCrearDirectorioActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_botonCrearDirectorioActionPerformed

    private void botonBorrarDirectorioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonBorrarDirectorioActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_botonBorrarDirectorioActionPerformed

    private void textoDirectorioLocalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_textoDirectorioLocalActionPerformed

        jTreeLocal.setModel(OperacionesInterfaz.treeFinal(textoDirectorioLocal.getText(), false, entrada, salida));
    }//GEN-LAST:event_textoDirectorioLocalActionPerformed

    private void textoDirectorioRemotoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_textoDirectorioRemotoActionPerformed
        jTreeRemoto.setModel(OperacionesInterfaz.treeFinal(textoDirectorioRemoto.getText(), true, entrada, salida));
    }//GEN-LAST:event_textoDirectorioRemotoActionPerformed

    private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
        salida.println("salir");
        salida.flush();
        try {
            entrada.readLine();
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.exit(0);
    }//GEN-LAST:event_jMenuItem3ActionPerformed

    private void crearUsuarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_crearUsuarioActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_crearUsuarioActionPerformed

    private void borrarUsuarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_borrarUsuarioActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_borrarUsuarioActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new PantallaManejoFTP().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem borrarUsuario;
    private javax.swing.JButton botonBorrarDirectorio;
    private javax.swing.JButton botonBorrarFichero;
    private javax.swing.JButton botonCrearDirectorio;
    private javax.swing.JButton botonDescargarFichero;
    private javax.swing.JButton botonSubirFichero;
    private javax.swing.JMenuItem crearUsuario;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenu jMenu4;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JTree jTreeLocal;
    private javax.swing.JTree jTreeRemoto;
    private javax.swing.JTable tablaDirectorioLocal;
    private javax.swing.JTable tablaDirectorioRemoto;
    private javax.swing.JTextField textoDirectorioLocal;
    private javax.swing.JTextField textoDirectorioRemoto;
    // End of variables declaration//GEN-END:variables
}
